<template>
  <view>
    <u-navbar  title="骑手学习中心" :backgroundColor="'transparent'"></u-navbar>
    <x-video ref="videoPlayer" :src="videoSrc" :progress="progress" @play="videoPlay" :showRate="false"
             @pause="videoPause" @ended="videoEnded" @timeupdate="videoTimeUp" @loadeddata="videoLoaded"
             @seeking="videoSeeking" @seeked="videoSeeked" @error="videoError"/>

  </view>
</template>

<script>
import XVideo from "../../uni_modules/x-video/components/x-video/x-video.vue";
import {isEmpty} from "@/utils";

export default {
  components:{XVideo},
  data() {
    return {
      videoSrc:"",
      progress:0,
      videoInfo:uni.getStorageSync('videoInfo'),
      recordId:uni.getStorageSync('videoInfo').recordId,
      previousNumber:0,
      currentNumber:0,
      durationTime:0
    }
  },
  onLoad(query) {
    this.videoSrc = (window.location.href.includes('/xsdcrm-wechat-ui/')?consts.API_BASE_URL+'/xsdcrm-static/upload/': '/profile/') + this.videoInfo.courseUrl
    console.log(this.videoSrc)
    this.$nextTick(()=>{
      this.progress  = (this.videoInfo.duration && this.videoInfo.duration<this.videoInfo.courseSize)?parseInt(this.videoInfo.duration)/parseInt(this.videoInfo.courseSize):0
      this.$refs.videoPlayer.sliderDisabled = true
    })
    if (isEmpty(this.recordId)) {
        this.learningrecord()
    }
  },
  methods:{
    //新增骑手课程记录
    async learningrecord() {
      let riderInfo = uni.getStorageSync('riderInfo')
      let param = {
        ridersId:riderInfo.ridersId,
        ridersName:riderInfo.ridersId,
        courseId:this.videoInfo.id,
        courseName:this.videoInfo.courseName,
        siteId:riderInfo.sideId,
        stieName:riderInfo.sideName,
        status:2
      }
      let {code,data} = await this.$api.dormitory.learningrecord(param)
      if (code == 200) {
         this.recordId = data
      }
    },
    // 视频信息加载完成
    async videoLoaded(durationTime) {
      console.log('视频信息加载完成',durationTime)
      this.durationTime = durationTime
      // let riderInfo = uni.getStorageSync('riderInfo')
      // let param = {
      //   ridersPhone:riderInfo.ridersPhone,
      //   duration:durationTime,
      //   id:this.videoInfo.recordId,
      //   status:1
      // }
      // let {code,data} = await this.$api.dormitory.updateLearningrecord(param)
      // if (code == 200) {
      //
      // }
    },
    // 当前播放时间
    videoTimeUp(currentTime) {
      console.log('当前播放时间',currentTime)
      const result = Math.round(currentTime);
      // 如果之前的数字不同于当前数字，触发方法a
      if (this.previousNumber !== null && this.previousNumber !== result) {
        this.updateLearningrecord(result);  // 数字变化时触发方法a
      }
      // 更新之前的数字为当前数字
      this.previousNumber = result;
    },
    async updateLearningrecord(duration){
      let riderInfo = uni.getStorageSync('riderInfo')
      let param = {
        ridersPhone:riderInfo.ridersPhone,
        duration:duration>=this.durationTime?this.durationTime:duration,
        id:this.videoInfo.recordId,
        status:duration>=this.durationTime?'1':'2'
      }
      let {code,data} = await this.$api.dormitory.updateLearningrecord(param)
      if (code == 200) {
        console.log('记录成功',duration)
      }
    },
    // 点击原始播放
    videoPlay() {

    },
    // 正在拖动
    videoSeeking() {

    },
    // 拖动结束
    videoSeeked() {
      console.log('拖动结束')
    },
    // 触发暂停
    videoPause() {

    },
    // 播放结束
    videoEnded() {

    },
    // 播放错误
    videoError() {

    }
  }
}
</script>

<style scoped>
/deep/.slider-container{

}
</style>
